<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>百器纲目</title>
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
            font-size: 14px;
        }
        body {
            width: 100%;
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .container{
            display: flex;
            flex-direction: column;
            padding: 20px;
            width: 620px;
        }
        .content {
            display: flex;
            flex-direction: column;
            width: 600px;
            border: 1px solid #000;
            border-radius: 20px;
            padding: 10px;
        }
        .error_alert{
            position: fixed;
            left: 0px;
            top: 0px;
            background: #000;
            width: 100%;
            height: 100%;
            opacity: 0.83;
        }
        .tankuang{
            position: relative;
            background: #fff;
            top: 30%;
            width: 30%;
            height: 10%;
            border-radius: 5px;
            margin: 5% auto;
        }
        #error_msg{
            position: relative;
            top: 40%;
            text-align: center;
        }
        #header-right{
            position: absolute;
            width: 25px;
            height: 25px;
            border-radius: 5px;
            background: red;
            color: #fff;
            right: 5px;
            top: 5px;
            text-align: center;
        }
        #form1{
            display: flex;
            flex-direction: row;
            align-items: center;
            width: 100%;
            height: 40px;
        }
        #title {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            text-align: center;
            font-size: 24px;
            line-height: 120px;
            cursor: default;
            height: 120px;
            background-color: lightskyblue;
            font-weight: bold;
        }
        #file {
            width: 500px;
            padding: 5px 0;
        }
        #submit, #h-btn {
            width: 100px;
            padding: 2px 0;
            border-radius: 5px;;
            border-width: 1px;
        }
        .r-tit {
            margin-bottom: 5px;
        }
        .r-info {
            display: flex;
            flex-direction: row;
        }
        .info {
            width: 300px;
            padding: 2px;
            border: 1px solid #aaa;
        }
        .img, #result, #preview {
            width: 600px;
            height: 400px;
        }
        .img {
            margin: 10px 0;
        }
    </style>
</head>

<body>
    <div class="container">
        <div id='title'>百器纲目</div>
        <div class="content">
            <form enctype="multipart/form-data" name="form1" action="/detect/" method="POST" id='form1'>
                <input id="file" type="file" name="file" onchange="change()" />
                <input type="submit" value="上传图片" id='submit'>
            </form>
            
            {% if error %}
                <div class="error_alert" id='error_alert'>
                    <div class="tankuang">
                        <div id="header">
                            <br>
                            <p><div id="error_msg">{{msg}}</div></p>
                            <div id="header-right" onclick="hidder()">x</div>
                        </div>
                    </div>
                </div>
            {% endif %}
            {% if ok %}
                <p class='r-tit'>检测结果:</p>
                <div class="r-info">
                    <p class="info">Scalpel: {{A}}</p>
                    <p class="info">Straight Dissection Clamp: {{B}}</p> 
                </div>
                <div class="r-info">
                    <p class="info">Straight Mayo Scissor: {{C}}</p>
                    <p class="info">Curved Mayo Scissor: {{D}}</p>
                </div>
                
                <p class="img">
                    <img src={{base64_data}} id='result'>
                </p>
            {% else %}
            
                <p>预览:</p>
                    <p class="img">
                        <img id="preview" alt="" name="pic" />
                    </p>
            {% endif %}

            <form name="form2" action="/history/" method="POST">
                <input type="submit" value="历史记录" id='h-btn'>
            </form>
        </div>
        
    </div>
    

<script type="text/javascript">

function change() {
    var pic = document.getElementById("preview"),
        file = document.getElementById("file");
    console.log(file.value);
    var ext=file.value.substring(file.value.lastIndexOf(".")+1).toLowerCase();
    console.log(ext);
    // gif在IE浏览器暂时无法显示
    if(ext!='png' && ext!='jpg' && ext!='jpeg' && ext!='bmp' && ext!='JPG' && ext!='PNG'){
        alert("图片的格式必须为png或jpg或jpeg或bmp格式！"); 
        return;
    }
    html5Reader(file);
    }

function html5Reader(file){
    var file = file.files[0];
    var reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = function(e){
        var pic = document.getElementById("preview");
        pic.src=this.result;
    }
}

document.getElementById('error_alert').style.display="";

function hidder(){
    document.getElementById('error_alert').style.display="none";
}

</script>
</body>
